package com.kichina.parking.controller.error;

import com.kichina.parking.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 * @author xiaohan.yuan
 * */
@RestControllerAdvice
@Slf4j
public class WebGlobalExceptionHandler extends BaseController {

    /**
     * 数据源异常
     * */
    @ExceptionHandler({DataAccessException.class, CannotCreateTransactionException.class})
    public ResponseEntity<ModelMap> dataSourceException(DataAccessException e){
      log.error("捕获数据库相关全局异常!",e);
      return FailModelMap();
    }

    /**
     * 捕获数据校验异常
     * */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<ModelMap> illegalArgumentException(IllegalArgumentException e){
        log.error("捕获数据校验异常!{}",e.getMessage());
        return FailModelMap(e.getMessage());
    }


    /**
     * 捕获数据校验异常
     * */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<ModelMap> runtimeException(RuntimeException e){
        log.error("捕获到运行时异常!",e);
        return FailModelMap(e.getMessage());
    }
}
